package dyyx.util;

import dyyx.dto.RedisClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;


public abstract class RedisUtil {
	// private static final Random RAND = new Random();
	
	private static volatile RedisClient client;

	
	public static synchronized void connect(String type,String host,int port,String pwd)throws Exception{
		if(client!=null){
			throw new Exception("connected"); 
		}
		
		
		RedisClient clienttmp = new RedisClient( type, host, port, pwd);
		client = clienttmp;			
	}
	
	public static synchronized RedisClient getClient(){
		if(client==null){
			throw new RuntimeException("not connected"); 
		}
		return client;
	}
	
	public static synchronized void disconnect()throws Exception{
		if(client==null){
			throw new Exception("not connected"); 
		}
		// client.connection.close();
		
		Jedis jedis = client.jedis;
		if(jedis!=null) {
			jedis.close();
		}

		JedisCluster jedisCluster = client.jedisCluster;
		if(jedisCluster!=null) {
			jedisCluster.close();
		}

		
		client = null;
	}
	
	

}
